package com.waitingresult.thread.callable;

import java.util.ArrayList;

/**
 * All right reserved,Designed By WangShuai
 *
 * @author : WangShuai
 * @version : V1.0
 * @ClassName : ThreadLockDemoMain
 * @Description : 多线程环境下的死锁演示
 * @date : 2022/9/6 19:29
 */
public class ThreadLockDemoMain {
    public static void main(String[] args) {
        Object o1 = new Object();
        Object o2 = new Object();
//        new Thread(new ThreadDemo(o1,o2,true)).start();
//        new Thread(new ThreadDemo(o1,o2,false)).start();

        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        for (Integer integer : list) {
           // list.add(10+integer);
        }


        // 时效性，Cookie，Token 不可修改

        // 了解那几种加密方式

        // Security 的默认加密方式。

        // Redis set a = 1 TTL  60min 原子性操作

        // Redis

        // Redis 拒绝策略

        // 算法
    }
}
class ThreadDemo implements Runnable{
    private Object object1;
    private Object object2;
    private Boolean flag;
    public ThreadDemo(Object obj1,Object obj2,Boolean flag){
        this.object1 = obj1;
        this.object2 = obj2;
        this.flag = flag;
    }
    @Override
    public void run() {
        //让两个线程分别执行 if 和else
        if(flag){
            try {
                System.err.println(Thread.currentThread().getName()+"标识为Ture,开始执行代码");
                synchronized (object1){
                    System.err.println(Thread.currentThread().getName()+"标识为Ture,锁定了Obj1,执行业务");
                    Thread.sleep(1000);
                    System.err.println(Thread.currentThread().getName()+"标识为Ture,锁定了Obj1,尝试获取Obj2的锁");
                    synchronized (object2){
                        System.err.println(Thread.currentThread().getName()+"获取了两把锁");
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else{
            try {
                System.err.println(Thread.currentThread().getName()+"False,开始执行代码");
                synchronized (object2){
                    System.err.println(Thread.currentThread().getName()+"False,锁定了Obj2,执行业务");
                    Thread.sleep(1000);
                    System.err.println(Thread.currentThread().getName()+"False,锁定了Obj2,尝试获取Obj1的锁");
                    synchronized (object1){
                        System.err.println(Thread.currentThread().getName()+"获取了两把锁");
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
